/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

// pure universe where objects can exist and move
// it has limits and it is created by Nature at the beginning of time
var WorldSpace = new Class({
    Extends: GameObject,
    initialize: function(width,height){
        this.parent();
        this.width = width;
        this.height = height;
        this.type='world';
    },
    changeWorldParameters: function(width,height){
        this.width = width;
        this.height = height;
    }
});

var ClientWorldSpace = new Class({
        Extends: WorldSpace,
	initialize: function(width,height){
		this.parent(width,height);
		this.HTMLIncarnation = new Element('div',{'class': 'world'});
                this.worldWindow={width: 0,height: 0};
                this.scrollStep=2*game.standardWidth;
                this.viewport=$('viewport');
                this.verticalMove = new Fx.Tween(this.HTMLIncarnation, {duration: 300});
                this.horizontalMove = new Fx.Tween(this.HTMLIncarnation, {duration: 300});
	},
	start: function(){
          this.scrollOffset=0;
          this.HTMLIncarnation.injectInside(this.viewport);
          this.worldWindow.width=parseInt(this.viewport.getStyle('width'));
          this.worldWindow.height=parseInt(this.viewport.getStyle('height'));
          this.HTMLIncarnation.setStyle('width',this.width);
          this.HTMLIncarnation.setStyle('height',this.height);
    },
    changeWorldParameters: function(width,height){
        this.parent(width,height);
        this.HTMLIncarnation.setStyle('width',this.width);
        this.HTMLIncarnation.setStyle('height',this.height);
    },
    moveLeft: function(){
        var step=this.scrollStep;
        if(this.x-step+game.worldWidth<=this.worldWindow.width)
            step=this.x+game.worldWidth-this.worldWindow.width;
        if(step>0){
          this.horizontalMove.start('left',this.x,this.x-step);
          this.x=this.x-step;
        }
    },
    moveRight: function(){
        var step=this.scrollStep;
        if(this.x+step>0)
            step=(-1)*this.x;
        if(step>0){
          this.horizontalMove.start('left',this.x,this.x+step);
          this.x=this.x+step;
        }
    },
    moveUp: function(){
        var step=this.scrollStep;
        if(this.y-step+game.worldHeight<=this.worldWindow.height)
            step=this.y+game.worldHeight-this.worldWindow.height;
        if(step>0){
          this.verticalMove.start('top',this.y,this.y-step);
          this.y=this.y-step;
        }
    },
    moveDown: function(){
        var step=this.scrollStep;
        if(this.y+step>0)
            step=(-1)*this.y;
        if(step>0){
          this.verticalMove.start('top',this.y,this.y+step);
          this.y=this.y+step;
        }
    },
    resetIntoView: function(){
        if(this.x!=0){
            this.horizontalMove.set('left',0);
            this.x=0;
        }
        if(this.y!=0){
            this.verticalMove.set('top',0);
            this.y=0;
        }
    },
    getWorldWrapper: function(){
        return this.viewport;
    }
});